package com.dongdongshop.service.impl;
import com.dongdongshop.mapper.TbAddressMapper;
import com.dongdongshop.mapper.TbAreasMapper;
import com.dongdongshop.mapper.TbCitiesMapper;
import com.dongdongshop.mapper.TbProvincesMapper;
import com.dongdongshop.pojo.*;
import com.dongdongshop.service.TbAddressService;
import com.dongdongshop.vo.AddressVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@DubboService
public class TbAddressServiceImpl implements TbAddressService {
    @Autowired
    private TbAddressMapper tbAddressMapper;
    @Autowired
    private TbProvincesMapper provincesMapper;
    @Autowired
    private TbCitiesMapper citiesMapper;
    @Autowired
    private TbAreasMapper areasMapper;

    @Override
    public List<AddressVo> selectAddressByUserId(String username) {
        List<AddressVo> addressVoList = tbAddressMapper.selectByUsername(username);
        return addressVoList;
    }

    @Override
    public List<TbProvinces> selectProvinces() {
        return provincesMapper.selectByExample(null);
    }

    @Override
    public List<TbCities> selectCities(String id) {
        TbCitiesExample example = new TbCitiesExample();
        TbCitiesExample.Criteria criteria = example.createCriteria();
        criteria.andProvinceidEqualTo(id);
        return citiesMapper.selectByExample(example);
    }

    @Override
    public List<TbAreas> selectAreas(String id) {
        TbAreasExample example = new TbAreasExample();
        TbAreasExample.Criteria criteria = example.createCriteria();
        criteria.andCityidEqualTo(id);
        return areasMapper.selectByExample(example);
    }

    @Override
    public void addAddress(TbAddress address) {
        tbAddressMapper.insertSelective(address);
    }

    @Override
    public TbAddress selectAddressById(Long id) {
        return tbAddressMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateAddressById(TbAddress address) {
        tbAddressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public AddressVo selectAddressByAId(Long userId) {
        return tbAddressMapper.selectById(userId);
    }

    @Override
    public void toUpdateArea(Long id) {
        TbAddress add = tbAddressMapper.selectByPrimaryKey(id);
        TbAddressExample example = new TbAddressExample();
        TbAddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(add.getUserId());
        List<TbAddress> tbAddresses = tbAddressMapper.selectByExample(example);
        for (TbAddress address : tbAddresses) {
            address.setIsDefault("0");
            tbAddressMapper.updateByPrimaryKeySelective(address);
        }
        add.setIsDefault("1");
        tbAddressMapper.updateByPrimaryKeySelective(add);
    }

    @Override
    public void deleteAddressById(Long id) {
        tbAddressMapper.deleteByPrimaryKey(id);
    }
}
